//35pts
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int _36;
ll n,a[363636];
ll dp[405][405][405];
ll maxn[405][405];
ll intr(int x,int y){
	return (a[x]==a[y]?a[x]:0);
}
int main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	//~ cout<<(double)sizeof(dp)/1024/1024;
	scanf("%d",&_36);
	while(_36--){
		scanf("%d",&n);
		for(int i=1;i<=n;i=i+1){
			scanf("%lld",&a[i]);
		}
		a[0]=-36;
		memset(dp,-0x3f,sizeof(dp));
		memset(maxn,-0x3f,sizeof(maxn));
		dp[1][1][0]=maxn[1][1]=0;
		for(int i=2;i<=n;i=i+1){
			for(int j=1;j<=i;j=j+1){
				for(int k=0;k<=j-2;k=k+1){
					if(i==j){
						dp[i][j][k]=max(dp[i][j][k],maxn[j-1][k+1]+intr(i,k));
					}
					else{
						dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k]+intr(i,i-1));
					}
					maxn[i][j]=max(maxn[i][j],dp[i][j][k]);
				}
			}
		}
		ll ans=-0x3f3f3f3f3f3f3f3f;
		for(int i=1;i<=n;i=i+1){
			for(int j=0;j<=i-2;j=j+1){
				ans=max(ans,dp[n][i][j]);
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}
//                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
